package com.wms.common;

import com.wms.dao.SystemMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import static org.springframework.util.ObjectUtils.isEmpty;

@Component
public class Utils {
    @Autowired
    private SystemMapper systemMapper;

    public static Utils utils;
//    public boolean isNumeric(String str) {
//        try {
//            Double.parseDouble(str);
//            return true;
//        } catch (NumberFormatException e) {
//            return false;
//        }
//    }
    @PostConstruct
    public void init(){
        utils = this ;
        utils.systemMapper = this.systemMapper;
    }

    public static Long getDate(){
        return System.currentTimeMillis();
    }

    public static String getStringDate(){
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return currentDate.format(formatter);
    }
    public static String getTableNumber(String tableName,String parentNumber){
        Long len = Config.tableNumberLength.get(tableName);
        int numberLen = 3;
        if(len != null){
            numberLen = Math.toIntExact(len);
        }
        switch (tableName){
            case "goods":
                String number = utils.systemMapper.getTableMaxNumber("goods_number",tableName,parentNumber,0L);
                return parentNumber + getNumber(number,numberLen);
            case "in_store_idx":
                String numberIdx = utils.systemMapper.getTableMaxNumberIdx("in_number",tableName,numberLen);
                return getStringDate() + "-"+getNumber(numberIdx,numberLen);
            case "out_store_idx":
                String outNumberIdx = utils.systemMapper.getTableMaxNumberIdx("out_number",tableName,numberLen);
                return getStringDate() + "-"+getNumber(outNumberIdx,numberLen);
            default:
                return "0001";
        }
    }

    public static String getNumber(String number,int numberLen){
        if(isEmpty(number)){
            return getEmptyNumber(numberLen);
        }else{
            return groupNumber(number,numberLen);
        }
    }

    public static String getEmptyNumber(int len){
        String oriNumber = "00000001";
        return oriNumber.substring( oriNumber.length() - len);
    }

    public static String groupNumber(String number,int numberLen){
        String maxNumber = number.substring(number.length() - numberLen);
        int maxNum = Integer.valueOf(maxNumber) + 1;
        String oriNumber = "00000000";
        String newNumber = oriNumber + String.valueOf(maxNum);
        return newNumber.substring( newNumber.length() - numberLen);
    }

    public static boolean isBigDecimal(BigDecimal value){
        if(value == null){
            return true;
        }
        if(value.compareTo(BigDecimal.ZERO) == 0){
            return true;
        }

        return false;
    }
}
